﻿Option Strict On
Public Class MetaList
    Implements IDisposable

    Private disposedValue As Boolean = False        ' To detect redundant calls
    Private _metaListValues As MetaListValues = New MetaListValues()
    Private mq As MetaQuery
    Private _filter As String

#Region " Property Field "
    Private _listCd As String
    Private _listDescr_PRM As String
    Private _listDescr_ALT As String
    Private _listId As Guid
    Private _inactive As Boolean
    Private _readonly As Boolean
#End Region
    ''' <summary>
    ''' MetaList classe that contains, all the value and the description of the list.
    ''' </summary>
    ''' <param name="listName"></param>
    ''' <param name="filter "></param>
    ''' <remarks>Populate by the MetaCommand Metalists</remarks>
    Public Sub New(ByVal listName As String, Optional ByVal filter As String = Nothing)
        Me._listCd = listName
        Me._filter = filter
        Me.FillMetaListInfo()
        Me.FillMetaListValues()
    End Sub

    Public Sub FillMetaListInfo()
        'mq = MetaQuery.GetMetaQueryByName("MetaLists", "Meta")
        mq = MetaQuery.GetMetaQueryByName("MetaLists")
        mq.SQLWhere = String.Format("ListCd = '{0}'", Me.ListCd)
        If Me.Filter IsNot Nothing Then
            mq.SQLWhereContext = Me.Filter
        End If
        mq.FillResultQuery()
        For Each row As DataRow In mq.ResultQuery.Rows
            Me.Inactive = CBool(row("Inactive"))
            Me.ListId = CType(row("ListID"), Guid)
            Me.ListCd = row("ListCd").ToString
            Me.ListDescr_ALT = row("ListDescr_ALT").ToString
            Me.ListDescr_PRM = row("ListDescr_PRM").ToString
            Me.Readonly = CBool(row("Readonly"))
        Next
    End Sub
    Public Sub FillMetaListValues()
        Dim mlv As MetaListValue
        'Dim gid As Guid

        'mq = MetaQuery.GetMetaQueryByName("MetaListValues", "Meta")
        mq = MetaQuery.GetMetaQueryByName("MetaListValues")
        mq.SQLWhere = String.Format("ListID_FK = '{0}'", Me.ListId)
        If Me.Filter IsNot Nothing Then
            mq.SQLWhereContext = Me.Filter
        End If
        mq.FillResultQuery()
        For Each row As DataRow In mq.ResultQuery.Rows
            mlv = New MetaListValue
            mlv.Inactive = CBool(row("Inactive"))
            mlv.ListOrder = CInt(row("ListOrder"))
            mlv.ListValueCd = row("ListValueCd").ToString
            mlv.ListValueDescr_ALT = row("ListValueDescr_ALT").ToString
            mlv.ListValueDescr_PRM = row("ListValueDescr_PRM").ToString
            mlv.ListValueID = CType(row("ListValueID"), Guid)
            'gid = New Guid(row("ListValueID").ToString)
            If Not Me._metaListValues.ContainsKey(mlv.ListValueID) Then
                Me._metaListValues.Add(mlv.ListValueID, mlv)
            End If
        Next

    End Sub
    Public Property [Readonly]() As Boolean
        Get
            Return _readonly
        End Get
        Set(ByVal value As Boolean)
            _readonly = value
        End Set
    End Property
    Public Property Inactive() As Boolean
        Get
            Return _inactive
        End Get
        Set(ByVal value As Boolean)
            _inactive = value
        End Set
    End Property
    Public Property ListCd() As String
        Get
            Return _listCd
        End Get
        Set(ByVal value As String)
            _listCd = value
        End Set
    End Property
    Public Property ListDescr_ALT() As String
        Get
            Return _listDescr_ALT
        End Get
        Set(ByVal value As String)
            _listDescr_ALT = value
        End Set
    End Property
    Public Property ListDescr_PRM() As String
        Get
            Return _listDescr_PRM
        End Get
        Set(ByVal value As String)
            _listDescr_PRM = value
        End Set
    End Property
    Public Property Filter() As String
        Get
            Return _filter
        End Get
        Set(ByVal value As String)
            _filter = value
        End Set
    End Property
    Public Property ListId() As Guid
        Get
            Return _listId
        End Get
        Set(ByVal value As Guid)
            _listId = value
        End Set
    End Property
    Public ReadOnly Property MetaListValues() As MetaListValues
        Get
            Return _metaListValues
        End Get
    End Property

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: free other state (managed objects).
                _metaListValues = Nothing
                mq = Nothing
            End If
            ' TODO: free your own state (unmanaged objects).
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class

Public Class MetaListValue

#Region " Property Field "
    Private _listValueID As Guid
    Private _listValueCd As String
    Private _listValueDescr_PRM As String
    Private _listValueDescr_ALT As String
    Private _listOrder As Integer
    Private _inactive As Boolean
    Private _readonly As Boolean
#End Region

#Region " Property "
    Public Property [Readonly]() As Boolean
        Get
            Return _readonly
        End Get
        Set(ByVal value As Boolean)
            _readonly = value
        End Set
    End Property
    Public Property Inactive() As Boolean
        Get
            Return _inactive
        End Get
        Set(ByVal value As Boolean)
            _inactive = value
        End Set
    End Property
    Public Property ListOrder() As Integer
        Get
            Return _listOrder
        End Get
        Set(ByVal value As Integer)
            _listOrder = value
        End Set
    End Property
    Public Property ListValueCd() As String
        Get
            Return _listValueCd
        End Get
        Set(ByVal value As String)
            _listValueCd = value
        End Set
    End Property
    Public Property ListValueDescr_ALT() As String
        Get
            Return _listValueDescr_ALT
        End Get
        Set(ByVal value As String)
            _listValueDescr_ALT = value
        End Set
    End Property
    Public Property ListValueDescr_PRM() As String
        Get
            Return _listValueDescr_PRM
        End Get
        Set(ByVal value As String)
            _listValueDescr_PRM = value
        End Set
    End Property
    Public Property ListValueID() As Guid
        Get
            Return _listValueID
        End Get
        Set(ByVal value As Guid)
            _listValueID = value
        End Set
    End Property
#End Region


End Class

''' <summary>
''' Dictionnary collection of MetaValues 
''' </summary>
''' <remarks>Populate by the MetaCommand MetalistValues. Dictionnary collection have been use cause is more effecient for Lookup process</remarks>
Public Class MetaListValues
    Inherits Dictionary(Of Guid, MetaListValue)

End Class








